package view;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MetaReportDAO {
	private DBConnect con = null;
    private long lnoexist;
        
    public MetaReportDAO (DBConnect incon)
    {
        con = incon;
        try
        {
            con.getConnection().setAutoCommit(false);
        }
        catch (SQLException e)
        {
        
        }
    }
    
    public String ViewOracleDriver()
    {        
    	StringBuffer metaDriverReport = new StringBuffer(1000);
    	
        try
        {
        	//query database requesting metaData
        	DatabaseMetaData md = con.getConnection().getMetaData();
        	// print information about the driver:
        	metaDriverReport.append("Name: " + md.getDriverName() + " Version: " + md.getDriverVersion() + "\n");
        }
        catch(Exception e)
        {
        	metaDriverReport.append("unknown Error in get Oracle Driver" +
    				"\n Message: " + e.getMessage());
            System.exit( 1 );
        }
       return metaDriverReport.toString();
    }
    
    public String ViewCriticalTable()
    {
    	StringBuffer metaCriticalTables = new StringBuffer(1000);
    	
        try
        {
	    	DatabaseMetaData metadata = con.getConnection().getMetaData();
	    	String strName="";
	    	String strType="";
	    	int intSize=0;
	        ResultSet resultSet = null;
	        
	        //get all columns related to cust_complaint table, then output them
	        resultSet = metadata.getColumns(null, con.getSchema(), "CUST_COMPLAINT", null);
	        metaCriticalTables.append("Table: CUST_COMPLAINT");
	    	while (resultSet.next()) {
	    	      strName = resultSet.getString("COLUMN_NAME");
	    	      strType = resultSet.getString("TYPE_NAME");
	    	      intSize = resultSet.getInt("COLUMN_SIZE");
	
	    	      metaCriticalTables.append("Column: " + strName + ", Field Type: " + strType + ", Field Size: " + intSize + "\n");
		    }
	    	
	    	//get all columns related to factory_order table, then output them
	        resultSet = metadata.getColumns(null, con.getSchema(), "FACTORY_ORDER", null);
	        metaCriticalTables.append("Table: FACTORY_ORDER");
	    	while (resultSet.next()) {
	    	      strName = resultSet.getString("COLUMN_NAME");
	    	      strType = resultSet.getString("TYPE_NAME");
	    	      intSize = resultSet.getInt("COLUMN_SIZE");
	
	    	      metaCriticalTables.append("Column: " + strName + ", Field Type: " + strType + ", Field Size: " + intSize + "\n");
		    }
	    	
	    	metaCriticalTables.append("Display complete! \n");
        }
        
        catch(Exception e)
        {
        	metaCriticalTables.append("unknown Error in get Critical Table Report");
        	metaCriticalTables.append("\nMessage: " + e.getMessage());
            System.exit( 1 );
        }
    	return metaCriticalTables.toString();
    }
}
